<?php
/************************
* Simple News Engine 0.2.3
* author: Ragnaar <ragnaar@km.ru>
* home: http://code.google.com/p/sne-news
* date: 08.06.2011
*********************/
define('_VALID_SNE', '1');
define('SNE_PATH', dirname(__FILE__)); //определяем путь

//шаблоны
$pub_header_tpl = SNE_PATH."/inc/tpl/pub_header.tpl";
$pub_footer_tpl = SNE_PATH."/inc/tpl/pub_footer.tpl";

$title = "Upgrade SimpleNews Engine";
$header = "";
include $pub_header_tpl;

require_once(SNE_PATH.'/inc/config.php');
require_once(SNE_PATH.'/inc/connect.php');
require_once(SNE_PATH.'/inc/functions.php');
require_once(SNE_PATH.'/inc/version.php');

$versions = array('0.2.2', '0.2.4'); // 

if (isset($_GET['version']) && in_array($_GET['version'], $versions)) {
    $sne = $_GET['version'];
} else {
    $sne = ""; //по умолчанию
}

if ($sne == "0.2.2") {
    $query = prefix("ALTER TABLE `{prefix}news` ADD `cut` tinyint(1) NOT NULL AFTER `text`", $dbprefix);
    mysql_query($query) or die ("SQL Error - " . mysql_error());
    

	if (isset($_GET['cut'])){
	    $rows = mysql_query(prefix("SELECT `text`, `id`, `cut` FROM `{prefix}news` ORDER BY `id` DESC", $dbprefix));
		while($row = mysql_fetch_object($rows)){
		    $fullen = mb_strlen($row->text, 'UTF-8');
		    if($fullen > $pg_text_len){
                for ($length = $pg_text_len; $row->text[$length] !== '.' and $length; $length--);
                    $row->text =  substr($row->text, 0, $length+1)."<hr id=\"cut\">".substr($row->text, $length+1, $fullen);
				    $row->cut = 1;
            } else {
                $row->text = $row->text."<hr id=\"cut\">";
				$row->cut = 0;
            }
			$query = prefix("UPDATE `{prefix}news` SET `text` = '".mysql_real_escape_string($row->text)."', `cut` = '".$row->cut."' WHERE `id` = '".$row->id."';", $dbprefix);
			mysql_query($query) or die ("SQL Error - " . mysql_error());
			echo "Success!";
		}

	} else {
        $rows = mysql_query(prefix("SELECT `text`, `id`, `cut` FROM `{prefix}news` ORDER BY `id` DESC", $dbprefix));
        while($row = mysql_fetch_object($rows)){
            $row->text = $row->text."<hr id=\"cut\">";
		    $row->cut = 0;
        }
		$query = prefix("UPDATE `{prefix}news` SET `text` = '".mysql_real_escape_string($row->text)."', `cut` = '".$row->cut."' WHERE `id` = '".$row->id."';", $dbprefix);
		mysql_query($query) or die ("SQL Error - " . mysql_error());
		echo "Success!";
	}
}
if ($sne == "0.2.4"){
    $query = prefix("ALTER TABLE `{prefix}news` ADD `comment` tinyint(1) NOT NULL AFTER `cut`", $dbprefix);
    mysql_query($query) or die ("SQL Error - " . mysql_error());

    $query = <<<SQL
    CREATE TABLE `{prefix}comments` (
        `id` int(11) NOT NULL auto_increment,
        `num` int(11) NOT NULL,
        `date` datetime NOT NULL default '0000-00-00 00:00:00',
        `socnet` tinytext NOT NULL,
        `nid` int(11) NOT NULL,
        PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
SQL;

    $query = prefix($query , $dbprefix);
    mysql_query($query) or die ("SQL Error - " . mysql_error());
    echo "Success!";
}

?>

<h3>Upgrade database</h3>
	
	<ol>
		<li>Upgrade database from version 0.1 - 0.2.1 to 0.2.2
		    <ul>
    		    <li><a href="<?php echo $newsPath; ?>upgrade.php?version=0.2.2&amp;cut=1">with automatic cut inserting</a></li>
    	        <li><a href="<?php echo $newsPath; ?>upgrade.php?version=0.2.2">without automatic cut inserting</a></li>
    	    </ul>
		</li>
		<li><a href="<?php echo $newsPath; ?>upgrade.php?version=0.2.4">Upgrade database from version 0.2.2 to 0.2.4</a></li>
	</ol>

<?php

include $pub_footer_tpl;

?>
